<?php
namespace app\index\controller;
use think\captcha\Captcha;
use think\Controller;

class Login extends Controller
{
	//登录
    public function login()
    {    	
        
        return $this->fetch();
    }

    //登录验证
    public function login_do()
    {
        $account = input("post.account");
        $passwd  = input("post.passwd");
        if($account==""){
            $this->error("账号不能为空");
        }
        if($passwd==""){
            $this->error("密码不能为空");
        }

        $data = db('member')->where(['mobile'=>$account])->find();

        $verifyCode = input("verifyCode");

        $res = $this->check_verify($verifyCode);
        if($res!=true)$this->error("验证码不正确！");
        if(!$data){
            $this->error("账号或者密码错误！");
        }elseif($data['status']==0){
            $this->error("该账号被禁止使用!");
        }else{
            if($data['password']!=md5($passwd)){
                $this->error("账号或者密码错误！");
            }else{
                session("member_info",$data);
                $l['loginip']   = get_client_ip();
                $l['logintime'] = time();
                db('member')->where("mobile='$account'")->update($l);
                $this->redirect('Index/Index');
            }
        }
    }

    //注册
    public function registered()
    {
        //父分类
        $industry_category = db('industry_category')->where(['parent_id'=>'0'])->select();

        //帮助语言
        $value = db('set')->where(['id'=>'8'])->value('value');


        $this->assign('industry_category',$industry_category);
        $this->assign('value',$value);
    	return $this->fetch();
    }

    //注册动作
    public function registered_do()
    {
        //行业id 
        $industry = input('post.industry');
        $industry = db('industry')->where(['id'=>$industry])->find();
        
        $industry_name = '';
        $category_result= db('industry_category')->where(['id'=>$industry['cid']])->find();
        if(empty($category_result['parent_id'])){
            $industry_name = $category_result['name'];
        }else{
            $parent_cateogy_result = db('industry_category')->where(['id'=>$category_result['parent_id']])->value('name');
            $industry_name = $parent_cateogy_result.'/'.$category_result['name'];
        }
        $industry_name .=  ('/'.$industry['name']); 


        $data['mobile'] = input('post.account');
        $data['password'] = input('post.passwd');
        $data['password'] = md5($data['password']);
        $data['company'] = input('post.company');
        $data['contact'] = input('post.contact');
        $data['user_industry'] = $industry_name;
        $data['users_location'] = input('post.users_location');
        $data['telephone'] = input('post.telephone');
        $data['province'] = input('post.province');
        $data['city'] = input('post.city');
        $data['area'] = input('post.area');
        $data['qq'] = input('post.qq');
        $data['email'] = input('post.email');
        if(empty($data['province']))$this->error('请选择所在地区！');
        if(empty($data['city']))$this->error('请选择所在地区！');
        if(empty($data['area']))$this->error('请选择所在地区！');
        $verifyCode = input("verifyCode");

        $member_result_find = db('member')->where(['mobile'=>$data['mobile']])->find();
        if(!empty($member_result_find))$this->error('该账号已注册过！');

        $res = $this->check_verify($verifyCode);
        if($res!=true)$this->error("验证码不正确！");

        $member_result = db('member')->insert($data);
        $mid = db('member')->getLastInsID();
        $data = db('member')->where(['id'=>$mid])->find();
        session("member_info",$data);
        $this->redirect('Index/Index');
    }

    public function captchaImg()
    {   $config =    [
            'codeSet'     =>'1234567890',
        // 验证码字体大小
            'fontSize'    =>    30,
        // 验证码位数
            'length'      =>    4,
        // 关闭验证码杂点
            'useNoise'    =>    true,
    ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    public function up_login(){
        session("member_info",null);
        $this->success('退出成功！');
    }

    /**
     * 检测输入的验证码是否正确
     *
     * @param $code 为用户输入的验证码字符串，
     * @param $id 多个验证码标识
     *
     */
    function check_verify($code, $id = ''){
        $captcha = new Captcha();
        return $captcha->check($code, $id);
    }


    /**
    *  注册
    */
    public function contract_display(){
        $id = input('id');
        
        $set_value = db('set')->where(['id'=>$id])->value('value');

        $this->assign('set_value',$set_value);
        return $this->fetch();
    }
}
